package com.example.demo.service.impl;

import com.example.demo.entity.Goods;
import com.example.demo.entity.Middle;
import com.example.demo.entity.User;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.mapper.MiddleMapper;
import com.example.demo.mapper.UserDao;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.GoodsService;
import com.example.demo.service.UserService;
import com.example.demo.test.ClassTest;
import com.example.demo.utils.ClassUtil;
import com.example.demo.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @Description
 * @Author chenhuiyu
 * @Date 2023/11/8
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private MiddleMapper middleMapper;
    @Autowired
    private ApplicationContext applicationContext;
    @Resource(name = "userDao")
    private UserDao userDao;

    @Override
    public User login(User user) {
        User userLogin = userMapper.selectUserByUsernameAndPassword(user.getUsername(), user.getPassword());
        if (Objects.equals(userLogin.getUsername(), user.getUsername()) && Objects.equals(userLogin.getPassword(), user.getPassword())) {
            return userLogin;
        } else {
            return null;
        }
    }

    @Override
    public List<Goods> findGoods(User user) {

        List<Goods> goods = goodsMapper.findGoodsByUserId(user.getId());
        List<Goods> goodsList = new ArrayList<>();
        for (Goods good : goods) {
            if (good.getFlag() == 0) {
                goodsList.add(good);
            }
        }
        return goodsList;
    }

    @Override
    public List<User> findUsers(User user) {
        List<User> userList = userMapper.findUserList(user);
//      List<User> userList = userDao.findUserList(user);
        return userList;
    }

    @Override
    public Result userToGoods(User user) {
        User u = userMapper.selectUserByUsernameAndPassword(user.getUsername(), user.getPassword());
        GoodsService goodsService;
        try {
            goodsService = (GoodsService) ClassUtil.invoke(applicationContext, "getBean", Class.forName("com.example.demo.service.GoodsService"));
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        Map<String, Goods> map = goodsService.findGoods(u);
        Result result = new Result();
        result.setMessage(u.getUsername());
        result.setObject(map);
        return result;
    }

    @Override
    public User add(User user) {
        Middle middle = new Middle();
        Long l = userMapper.add(user);
        middle.setUserId(user.getId());
        middle.setGroupId(user.getGroupId() == null ? 1 : user.getGroupId());
        Long m = middleMapper.add(middle);
        if (l > 0) {
            return user;
        } else {
            return null;
        }
    }

    @Override
    public User findUserByName(User user) {
        User _user = userMapper.findUserByName(user.getUsername());
        return _user;
    }

}
